function isStr (v) {
  return Object.prototype.toString.call(v) === '[object String]'
}
// 注册app
exports.registerApp = function (mic, opts) {
  console.log('registerApp', mic, opts)
  let apps = mic.apps
  let {name, el, app, host, type} = opts
  if (!name || name === '' || !isStr(name)) {
    throw new Error('[ERR]：注册 App name 必填，类型位字符串')
  }
  // app 列控制
  if (Object.prototype.hasOwnProperty.call(apps, name)) {
    // 合并
    console.log('APP', 'registerApp', '合并app')
    apps[name] = Object.assign(apps[name], {
      el: el,
      name: name,
      app: app,
      host: host,
      type: type || 'vue'
    })
  } else {
    console.log('APP', 'registerApp', '新增app')
    // 新增
    apps[name] = {
      el: el,
      name: name,
      app: app,
      host: host,
      type: type || 'vue'
    }
  }
}
// 开启app
exports.startApp = function (mic, opts) {
  console.log('startApp', mic, opts)

  return new Promise((resolve, reject) => {
    // app 集合
    const apps = mic.apps || {}
    // 当前启动的 App 名称
    const name = opts.name
    if (!name || name === '' || name.length <= 0) {
      reject('app name 不能为空')
      return
    }
    const app = apps[name]
    if (app.instance) {
      console.log(`${name} 项目已经被挂载，项目是 ${app} `)
      return app.instance
    }
    // 检查元素是否存在
    let el = document.getElementById(app.el)
    console.log('获取的元素是', el)
    if (!el) {
      reject(`挂载 ${name} App 获取DOM元素失败 -- ${el}`)
      return
    }
    if (app.type === 'react') {
       app.app.init()
       app.instance = true
      // 模拟置为true
      resolve(true)
    } else {
      app.instance = new mic._vue(app.app)
      const vm = app.instance.$mount('#' + app.el)
      if (vm) {
        console.log('startApp', name + ' Vue：加载成功')
        resolve(vm)
      } else {
        // 失败
        console.log('startApp', name + ' Vue：加载失败')
        reject(vm)
      }
    }
  })
}
// 获取 app 实例
exports.getInstance = function (mic, opts) {
  const name = opts.appName || ''
  const app = mic.apps[name]
  if (app) {
    return app.instance
  } else {
    return false
  }
}
